//
//  HPVerifyCurrentMobileViewController.m
//  HPZZTong
//
//  Created by Handpay on 14-3-27.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPVerifyCurrentMobileViewController.h"
#import "HPConditionTextField.h"
#import "ZZTongUtility.h"

@interface HPVerifyCurrentMobileViewController ()
@property(nonatomic, retain)HPConditionTextField* verifyCodeTextField;
@property(nonatomic, retain)HPConditionTextField* pwdTextField;
@end

@implementation HPVerifyCurrentMobileViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"修改手机号码";
    [super viewDidLoad];
    
    
    self.mobileTableView.dataSource = self;
    
    [self.nextButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.nextButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
}

- (void)dealloc {
    [_mobileTableView release];
    [_nextButton release];
    [super dealloc];
}

- (void)viewDidUnload {
    self.pwdTextField = nil;
    self.verifyCodeTextField = nil;
    
    [self setMobileTableView:nil];
    [self setNextButton:nil];
    [super viewDidUnload];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 44)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    
    HPConditionTextField* textField = [[HPConditionTextField alloc] initWithFrame:CGRectMake(110, 0, 200, 44)];
    textField.textColor = kNormalFontColor;
    textField.font = kNormalFont;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    switch (indexPath.row) {
        case 0:
            desLabel.text = @"原手机号码";
            textField.userInteractionEnabled = NO;
            textField.text = [ZZTongUtility maskMobile:ZZT_CLIENG.accountInfo.mobile];
            break;
        case 1:
            desLabel.text = @"短信验证码";
            textField.conditionType = HPConditionType_Numeric;
            textField.maxLength = 6;
            self.verifyCodeTextField = textField;
            
            UIButton* getVerifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
            getVerifyButton.frame = CGRectMake(215, 2, 100, 40);
            [getVerifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            UIColor* color = kVerChannel == VerChannelHeXin ? [UIColor whiteColor] : kNormalFontColor;
            [getVerifyButton setTitleColor:color forState:UIControlStateNormal];
            [getVerifyButton setBackgroundImage:[[UIImage imageNamed:@"smallbutton.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:0] forState:UIControlStateNormal];
            [getVerifyButton addTarget:self action:@selector(getVerifyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            getVerifyButton.titleLabel.font = kNormalFont;
            [cell addSubview:getVerifyButton];
            
            textField.frame = CGRectMake(110, 0, 105, 44);
            break;
        case 2:
            desLabel.text = @"登录密码";
            textField.secureTextEntry = YES;
            self.pwdTextField = textField;
            break;
        default:
            break;
    }
    
    [cell addSubview:desLabel];
    [desLabel release];
    
    [cell addSubview:textField];
    [textField release];
    
    return [cell autorelease];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [ZZT_CLIENG resetVerifyBtnState];
    self.verifyCodeTextField.text = @"";
    self.pwdTextField.text = @"";
}
- (void)getVerifyButtonClick:(UIButton*)button{
    NSDictionary* params = @{KEY_ACTION:@"oldPhone",
                             KEY_ACCOUNT:DES_ENC(ZZT_CLIENG.accountInfo.mobile),
                             KEY_CHANNEL: [ZZT_CLIENG getChannel]};
    [ZZT_CLIENG doSendVerifyCode:params withButton:button];
}

- (IBAction)nextButtonClick:(id)sender {
    if (![ZZTongUtility checkIsInput:self.verifyCodeTextField tips:@"短信验证码"]) {
        [self.verifyCodeTextField becomeFirstResponder];
        return;
    }
    
    if (![ZZTongUtility checkLength:self.verifyCodeTextField tips:@"请输入6位短信验证码" minLen:6 maxLen:6]) {
        [self.verifyCodeTextField becomeFirstResponder];
        return;
    }
    
    if (![ZZTongUtility checkIsInput:self.pwdTextField tips:@"登录密码"]) {
        [self.pwdTextField becomeFirstResponder];
        return;
    }
    [self.pwdTextField resignFirstResponder];
    [self.verifyCodeTextField resignFirstResponder];
    
    ZZTongClientEngine* zzt = ZZT_CLIENG;
    
    NSDictionary* params = @{KEY_PHONE: DES_ENC(zzt.accountInfo.mobile),
                             KEY_VERIFYCODE: self.verifyCodeTextField.text,
                             KEY_ACTION: @"oldPhone",
                             @"pwd": DES_ENC(self.pwdTextField.text),
                             KEY_CHANNEL:[zzt getChannel],
                             @"account":DES_ENC(zzt.accountInfo.account)};
    LOG(@"params:%@", params);
#if ZZT_FOR_TEST
    [self responseCallBackSuccess:NET_ACTION_CHECK_MOBILE withData:nil];
#else
    [zzt.netEngine sendRequest:NET_ACTION_CHECK_MOBILE withParams:params withDelegate:self];
    [zzt.uiEngine showNetWaiting];
#endif

}

- (void)responseCallBackSuccess:(NSString *)action withData:(NSDictionary *)respData{
    [HPUIENG removeWaiting];
    if ([action isEqualToString:NET_ACTION_CHECK_MOBILE]) {
        [HPUIENG showScreen:SCR_MODIFY_MOBILE];
    }
}
@end
